// 引入express
// import express from 'express';
const express = require("express");
const printAll = require("./examples/printAll")
//实例化express
//创建接口get请求
const mysql = require('mysql');
//实例化express
const app = express()
// 创建数据库连接
const connection = mysql.createConnection({
  // host: 'localhost',
  // user: 'root',
  host: '43.138.105.103',
  user: 'conistydy_user',
  password: '123456',
  database: 'conistydy_user'
})

//档案管理单选的接口
app.get('/getExamination_s', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM examination_s ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM examination_s `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//档案管理多选的接口
app.get('/getExamination_ss', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM examination_ss ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM examination_ss `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//档案管理判断的接口
app.get('/getExamination_pd', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM examination_pd ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM examination_pd `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//数据库单选
app.get('/getDatabase_s', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM database_s ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM database_s `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//数据库多选的接口
app.get('/getDatabase_ss', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM database_ss ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM database_ss `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//数据库判断的接口
app.get('/getDatabase_pd', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM database_pd ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM database_pd `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//中间件单选
app.get('/getMiddleware_s', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM middleware_s ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM middleware_s `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//中间件多选的接口
app.get('/getMiddleware_ss', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM middleware_ss ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM middleware_ss `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//中间件判断的接口
app.get('/getMiddleware_pd', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM middleware_pd ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM middleware_pd `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//安规单选
app.get('/getSafetyRegulations_s', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM safety_regulations_s ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM safety_regulations_s `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//安规多选的接口
app.get('/getSafetyRegulations_ss', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM safety_regulations_ss ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM safety_regulations_ss `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})
//安规件判断的接口
app.get('/getSafetyRegulations_pd', function (req, res) {
  let query = null
  if (req.query.size) {
    query = `SELECT * FROM safety_regulations_pd ORDER BY RAND() LIMIT ${req.query.size};`
  } else {
    query = `SELECT * FROM safety_regulations_pd `
  }
  connection.query(query, (error, results, fields) => {
    if (error) {
      res.send({
        code: 500,
        message: '失败'
      })
    } else {
      let result = {
        data: results,
        code: 200,
        message: '成功'
      }
      res.json(result)
    }
  })
})

app.get('/', function (req, res) {
  const print = printAll('214343')

  res.send('你好呀 !Hello World!')
})
app.post('/api', function (req, res) {
  res.send('你好呀 !Hello World!post')
})


//创建服务器
app.listen(5521, () => {
  console.log('server is running on port 5521')
})